package com.zjzhang.mqtt;

import java.util.Enumeration;
import java.util.Iterator;

import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;
import com.novell.ldap.util.Base64;

public class UserAuthenticate {
	public static boolean authUser(String userid,String userpasswd) {
		String ldapHost = "113.57.153.83";
		int ldapPort = LDAPConnection.DEFAULT_PORT;
		 String searchBase = "ou=People,dc=ailk,dc=com";
		 String searchFilter =  String.format("uid=%s",userid);;

		String verifyDN = String.format("uid=%s,ou=People,dc=ailk,dc=com",userid);
		int searchScope = LDAPConnection.SCOPE_SUB;
		LDAPConnection lc = new LDAPConnection();

		try {
			//MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
			//messageDigest.update(userpasswd.getBytes());
			lc.connect(ldapHost, ldapPort);
			lc.bind(LDAPConnection.LDAP_V3,verifyDN, userpasswd.getBytes());
			LDAPSearchResults searchResults = lc.search(searchBase, searchScope, searchFilter, null, false);

			while (searchResults.hasMore()) {
				LDAPEntry nextEntry = null;
				try {
					nextEntry = searchResults.next();
				} catch (LDAPException e) {
					System.out.println("Error: " + e.toString());
					if (e.getResultCode() == LDAPException.LDAP_TIMEOUT
							|| e.getResultCode() == LDAPException.CONNECT_ERROR) {
						break;
					} else {
						continue;
					}

				}
				LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
				@SuppressWarnings("unchecked")
				Iterator<LDAPAttribute> allAttributes = attributeSet.iterator();
				while (allAttributes.hasNext()) {
					LDAPAttribute attribute = allAttributes.next();
					String attributeName = attribute.getName();
					@SuppressWarnings("unchecked")
					Enumeration<String> allValues = attribute.getStringValues();
					if (null == allValues) {
						continue;
					}
					while (allValues.hasMoreElements()) {
						String value = allValues.nextElement();
						if (!Base64.isLDIFSafe(value)) {
							// base64 encode and then print out
							value = Base64.encode(value.getBytes());
						}
						System.out.println("|---- ---- " + attributeName + " = " + value);
					}
				}
			}
			//LDAPAttribute attr = new LDAPAttribute("userPassword",userpasswd);
			//boolean correct = lc.compare(verifyDN, attr);
			return true;
		} catch (LDAPException e) {
			e.printStackTrace();
			if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) {
				System.err.println("Error: No such entry");
			} else if (e.getResultCode() == LDAPException.NO_SUCH_ATTRIBUTE) {
				System.err.println("Error: No such attribute");
			} else {
				System.err.println("Error: " + e.toString());
			}
		} finally {
			try {
				if (lc.isConnected()) {
					lc.disconnect();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
